<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="../../css/bootstrap-table.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="../../js/bootstrap.min.js"></script>
    <script src="../../js/bootstrap-table.min.js"></script>
    <script src="../../js/bootstrap-table-zh-CN.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>


<table id="tab">

</table>


<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">生产派工</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="container"
                     style="padding-bottom:10px ;box-shadow: rgba(144,146,154,0.46) 5px 5px; background-color: whitesmoke;text-align: center">
                    <div>
                        <h2>生产派工单</h2>
                    </div>
                    <div class="">
                        <form>
                            <div class="form-row">
                                <div class="col">
                                    派工单编号： <span id="manufactureId"></span>
                                </div>
                                <div class="col"> &nbsp;
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col">
                                    产品编号： <span id="productId"></span>
                                </div>
                                <div class="col">
                                    产品名称:<span id="productName"></span>
                                </div>
                            </div>

                            <div class="form-row">
                                <div class="col">
                                    描述： <span id="describe"></span>
                                </div>
                                <div class="col">
                                    数量:
                                    <span id="quantity"></span>
                                </div>
                            </div>
                            <table id="table1" style="width: 100%; margin-top: 20px">
                                <thead>
                                <tr style="background-color: #a4efef;">
                                    <th>工序名称</th>
                                    <th>设计工时数</th>
                                    <th>实际工时数</th>
                                    <th>设计工时成本</th>
                                    <th>实际工时成本</th>
                                    <th>设计物料成本</th>
                                    <th>实际物料成本</th>
                                    <th>登记审核</th>
                                    <th>交接审核</th>
                                </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                            <div class="form-row">
                                <div class="col">
                                    设计工时总成本： <span id="labour_cost_price_sum"></span>
                                </div>
                                <div class="col">
                                    设计物料总成本:
                                    <span id="module_cost_price_sum"></span>
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col">
                                    实际工时总成本： <span id="real_labour_cost_price_sum"></span>
                                </div>
                                <div class="col">
                                    实际物料总成本:
                                    <span id="real_module_cost_price_sum"></span>
                                </div>
                            </div>

                            <div class="form-row">
                                <div class="col">
                                    登记人： <span id="registrant1"></span>
                                </div>
                                <div class="col">
                                    登记时间:
                                    <span id="registrationTime1"></span>
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col">
                                    复核人： <span id="checker"></span>
                                </div>
                                <div class="col">
                                    复核时间:
                                    <span id="check_time"></span>
                                </div>
                            </div>
                        </form>
                        <div style="margin: 20px 0px">
                            备注：
                            <textarea class="form-control" id="remark1" readonly></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="exampleModal1" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel1">生产登记单</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="container"
                     style="padding-bottom:10px ;box-shadow: rgba(144,146,154,0.46) 5px 5px; background-color: whitesmoke;text-align: center">
                    <div>
                        <h2>生产登记单</h2>
                    </div>
                    <div class="">
                        <form>
                            <div class="form-row">
                                <div class="col">
                                    派工单编号： <span id="manufactureId1"></span>
                                </div>
                                <div class="col">
                                    工序名称： <span id="procedureName"></span>
                                    <input type="hidden" id="procedureId">
                                    <input type="hidden" id="parentId">
                                    <input type="hidden" id="parentId2">
                                    <input type="hidden" id="id2">
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col">
                                    负责人:<input id="principal" type="text" class="form-control" readonly/>
                                </div>
                                <div class="col">
                                    本次工时数:
                                    <input id="realLabourHourAmount" type="text" class="form-control" readonly/>
                                </div>
                            </div>
                            <table id="table2" style="width: 100%; margin-top: 20px">
                                <thead>
                                <tr style="background-color: #a4efef;">
                                    <th>序号</th>
                                    <th>物料名称</th>
                                    <th>物料编号</th>
                                    <th>单位成本</th>
                                    <th>本次数量</th>
                                </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                            <div class="form-row" style="margin:50px 0px">
                                <div class="col">
                                    复核人： <span id="checker1"></span>
                                </div>
                                <div class="col">
                                    复核时间:
                                    <span id="checkTime1"></span>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button id="submitApplications" type="button" onclick="submitAuditaa()"
                        class="btn btn-primary">审核通过
                </button>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="exampleModal3" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel5">工序交接</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" style="height: 500px">
                <div class="input-group mb-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="inputGroup-sizing-default">本工序合格数量</span>
                    </div>
                    <input type="number" class="form-control" aria-label="Sizing example input"
                           aria-describedby="inputGroup-sizing-default" id="number1" readonly>
                    <input type="hidden" id="number2">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="SubmitForHandoverReview()">提交
                </button>
            </div>
        </div>
    </div>
</div>


<script>
    $(function () {
        $("#tab").bootstrapTable({
            method: "post",
            contentType: "application/json",
            striped: true,
            pageNumber: 1,
            pageSize: 5,
            pagination: true,
            sidePagination: 'server',
            pageList: [1, 3, 5, 10],
            url: "/ProductionRegistrationAudit/queryProductionAudit",
            queryParams: function (params) {
                return {
                    // 计算页号(当前页)
                    pageNumber: (params.offset / params.limit) + 1,
                    // 计算页大小
                    pageSize: params.limit
                }
            },
            columns: [
                {title: '派序号编号', field: 'manufactureId', align: 'center',},
                {title: '产品编号', field: 'productId', align: 'center',},
                {title: '产品名称', field: 'productName', align: 'center',},
                {title: '数量', field: "amount", align: 'center'},
                {title: '登记时间', field: "registerTime", align: 'center'},
                {
                    title: '审核', field: 'on',
                    formatter: function (value, row, index) {
                        return "<button class='btn btn-primary'  data-toggle=\"modal\"  data-target=\"#exampleModal\" onclick='registrationAudit (" + row.manufactureId + ")'>审核</button>";
                    }

                }
            ]
        })
    })


    function registrationAudit(id) {
        $.ajax({
            type: "post",
            url: "/InternalScheduling/registerDispatching",
            data: "id=" + id,
            success: function (data) {
                $("#id").val(data.id);
                $("#manufactureId").text(data.manufactureId);
                $("#productId").text(data.productId);
                $("#productName").text(data.productName);
                $("#describe").text(data.productDescribe);
                $("#quantity").text(data.amount);
                $("#module_cost_price_sum").text(data.moduleCostPriceSum);
                $("#labour_cost_price_sum").text(data.labourCostPriceSum);
                $("#registrant1").text(data.register);
                $("#registrationTime1").text(data.registerTime);
                $("#checker").text(data.checker);
                $("#check_time").text(data.checkTime);
                let procedureList = data.procedureList;
                $("#table1 tbody").html("");
                for (let i = 0; i < procedureList.length; i++) {
                    $("#table1 tbody").append("<tr>" +
                        "<td>" + procedureList[i].procedureName + "</td>" +
                        "<td>" + procedureList[i].labourHourAmount + "</td>" +
                        "<td>" + (procedureList[i].realLabourHourAmount == null ? '' : procedureList[i].realLabourHourAmount) + "</td>" +
                        "<td>" + (procedureList[i].subtotal == null ? '' : procedureList[i].subtotal) + "</td>" +
                        "<td>" + (procedureList[i].realSubtotal == null ? '0' : procedureList[i].realSubtotal) + "</td>" +
                        "<td>" + (procedureList[i].moduleSubtotal == null ? '' : procedureList[i].moduleSubtotal) + "</td>" +
                        "<td>" + (procedureList[i].realModuleSubtotal == null ? '0' : procedureList[i].realModuleSubtotal) + "</td>" +
                        "<td>" + (procedureList[i].procedureFinishTag == 'g004-1' ?
                            '<a href="#" class=\"btn btn-outline-success \" data-target=\"#exampleModal1\" ' +
                            ' onclick="checkIn(' + procedureList[i].parentId + ",'" + procedureList[i].procedureId + "'," +')">审核</a>' :
                            (procedureList[i].procedureFinishTag == 'g004-3' ? '已完成' : '')) + "</td>" +
                        "<td>" + (procedureList[i].procedureTransferTag === 'g005-0' ? '' :
                            (procedureList[i].procedureTransferTag == 'g005-1' ? '<a href="#" class=\"btn btn-outline-warning\" ' +
                                'onclick="TransferOfAudit(' + "'" + procedureList[i].procedureId + "'," + procedureList[i].parentId + "," + procedureList[i].detailsNumber + ')" ' + 'data-target=\"#exampleModal3\">交接审核</a>' : '已完成')) + "</td>" +
                        "</tr>");

                }
                var realModuleCostPriceSum=0;
                var realLabourCostPriceSum=0;
                $("#table1 tbody tr").each(function (i,n){
                    realLabourCostPriceSum+=parseInt($(n).find("td").eq(4).text());
                    realModuleCostPriceSum+=parseInt($(n).find("td").eq(6).text());
                })
                $("#real_module_cost_price_sum").text(realModuleCostPriceSum);
                $("#real_labour_cost_price_sum").text(realLabourCostPriceSum);

            }

        })
    }

    function checkIn(id, procedure_id) {
            $.ajax({
                data: "id=" + id + "&procedure_id=" + procedure_id,
                type: "post",
                url: "/ProductionRegistrationAudit/checkInQueryProceduring",
                success: function (data) {
                    $("#parentId").val(data.parentId)
                    $("#principal").val(data.procedureResponsiblePerson);
                    $("#manufactureId1").text($("#manufactureId").text());
                    $("#procedureName").text(data.procedureName);
                    $("#procedureId").val(data.procedureId);
                    $("#realLabourHourAmount").val((data.labourHourAmount == null ? '0.0' :
                        data.labourHourAmount));
                    $("#checkTime1").text(getTime());
                    $("#table2 tbody").html("")
                    for (let i = 0; i < data.procedureModulingObj.length; i++) {
                        let list = data.procedureModulingObj[i];
                        $("#table2 tbody").append("<tr>" +
                            "<td>" + list.id + "</td>" +
                            "<td>" + list.productName + "</td>" +
                            "<td>" + list.productId + "</td>" +
                            "<td>" + list.costPrice + "</td>" +
                            "<td>" + list.amount + "</td>" +
                            "</tr>")
                    }
                    $("#exampleModal1").modal("show");
                }
            })

    }

    $(function () {
        $.ajax({
            type: "post",
            url: "/plan/queryRegistrant",
            success: function (data) {
                $("#checker1").text(data.loginId);
            }
        })
    })


    function getTime() {
        var d = new Date();
        var year = d.getFullYear()  //获取年
        var month = d.getMonth() + 1;  //获取月，从 Date 对象返回月份 (0 ~ 11)，故在此处+1

        var days = d.getDate() //获取日期
        var hour = d.getHours()   //获取小时
        var minute = d.getMinutes()  //获取分钟
        var second = d.getSeconds()   //获取秒
        return year + "/" + month + "/" + days + " " + hour + ":" + minute + ":" + second
    }

    function submitAuditaa() {
        var obj = {
            procedureId: $("#procedureId").val(),
            parentId: $("#parentId").val(),
            checker: $("#checker1").text(),
            checkTime: $("#checkTime1").text(),
            manufactureId: $("#manufactureId1").text()
        }
        $.ajax({
            type: "post",
            url: "/ProductionRegistrationAudit/submitAuditaa",
            contentType: "application/json",
            data: JSON.stringify(obj),
            success: function (data) {
                if (data > 0) {
                    alert("审核成功");
                    $("#exampleModal").modal("hide");
                    $("#exampleModal1").modal("hide");
                    $("#tab").bootstrapTable("refresh");
                } else {
                    alert("审核失败");
                }


            }
        })
    }

    function TransferOfAudit(id, parentId, detailsNumber) {
        $.ajax({
            type: "post",
            url: "/ProductionRegistrationAudit/TransferOfAudit",
            data: "id=" + id + "&parentId=" + parentId,
            success: function (data) {
                $("#number1").val(data.realAmount)
            }
        })
        $("#number2").val(detailsNumber);
        $("#id2").val(id);
        $("#parentId2").val(parentId);
        $("#exampleModal3").modal("show");
    }

    function SubmitForHandoverReview() {
        if ($("#table1 tbody tr").eq(-1).find("td").eq(8).text() == '交接审核') {
            var manufactureObj={
                manufactureId:$("#manufactureId").text(),
                testedAmount:$("#number1").val(),
                realModuleCostPriceSum:$("#real_module_cost_price_sum").text(),
                realLabourCostPriceSum:$("#real_labour_cost_price_sum").text(),
                checker:$("#checker1").text(),
                checkTime:getTime(),
                productId:$("#parentId2").val(),
                productName:$("#id2").val()
            }
            $.ajax({
                type: "post",
                url: "/ProductionRegistrationAudit/SubmitHandoverReviewAndWarehousing",
                data:JSON.stringify(manufactureObj),
                contentType:"application/json",
                success:function (data){
                    if (data > 0) {
                        alert("审核成功")
                        $("#exampleModal").modal("hide");
                        $("#exampleModal3").modal("hide");
                        $("#tab").bootstrapTable("refresh");
                    }else{
                        alert("审核失败")
                    }
                }
            })

        } else {
            $.ajax({
                type: "post",
                data: "manufactureId=" + $("#manufactureId").text() + "&parentId=" + $("#parentId2").val() + "&procedureId=" + $("#id2").val(),
                url: "/ProductionRegistrationAudit/SubmitForHandoverReview",
                success: function (data) {
                    if (data > 0) {
                        alert("审核成功")
                        $("#exampleModal").modal("hide");
                        $("#exampleModal3").modal("hide");
                        $("#tab").bootstrapTable("refresh");
                    }else{
                        alert("审核失败")
                    }
                }
            })
        }

    }


</script>


</body>
</html>